//
//  HomeTransforOrderInfoFootView.m
//  ZJControlsClass
//
//  Created by mac on 2024/12/5.
//

#import "HomeTransforOrderInfoFootView.h"
#import "HomePublicChoosePhotoCollectionViewCell.h"
#import "ZJPublicUploadPhotoTableViewCell.h"
#import "SaTMNewWatermarkPhotoVc.h"
#import "TZImagePickerController.h"
#import "HomeChooseAlertView.h"
#import "HomeNetWorking.h"



@interface HomeTransforOrderInfoFootView ()<UICollectionViewDelegate,UICollectionViewDataSource,TZImagePickerControllerDelegate,ChooseAlertDelegate,UITextFieldDelegate>{
    NSMutableArray * _selectedAssets;
}

@property (nonatomic,strong) UILabel * chooseReceiverLabel;
@property (nonatomic,strong) UILabel * inputReceiverLabel;
@property (nonatomic,strong) UIImageView * receiverImg;
@property (nonatomic,strong) UIView * receiverV;

@property (nonatomic,strong) UILabel * chooseCSLabel;
@property (nonatomic,strong) UILabel * inputCSLabel;
@property (nonatomic,strong) UIImageView * CSImg;
@property (nonatomic,strong) UIView * CSV;

@property (nonatomic,strong) UILabel * noteLabel;
@property (nonatomic,strong) UITextField * noteTextF;


@property (nonatomic,strong) UILabel * photoLabel;
@property (nonatomic,strong) UICollectionView * collectionView;
@property (nonatomic,strong) NSMutableArray * photoArr;

@property (nonatomic,strong) UIView * KwindowView;
@property (nonatomic,strong) HomeChooseAlertView * alertView;

@property (nonatomic,strong) HomeUploadFileNetWorking * uploadImgManager;
@property (nonatomic,strong) NSMutableArray * uploadArr;

@end



@implementation HomeTransforOrderInfoFootView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        self.KwindowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        self.KwindowView.backgroundColor = [UIColor blackColor];
        self.KwindowView.alpha = 0;
        [kWindow addSubview:self.KwindowView];
        
        [self configUI];
    }
    return self;
}



-(void)configUI{
    
    [self addSubview:self.chooseReceiverLabel];
    [self addSubview:self.inputReceiverLabel];
    [self addSubview:self.receiverImg];
    [self addSubview:self.receiverV];
    
    [self addSubview:self.chooseCSLabel];
    [self addSubview:self.inputCSLabel];
    [self addSubview:self.CSImg];
    [self addSubview:self.CSV];
    
    [self addSubview:self.noteLabel];
    [self addSubview:self.noteTextF];
    
    [self addSubview:self.photoLabel];
    [self addSubview:self.collectionView];
    
    
    [self.chooseReceiverLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(ALIGN_WIDTH_PT(12));
        make.top.equalTo(self).with.offset(ALIGN_HEIGHT_PT(5));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(36)));
    }];
    
    [self.inputReceiverLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.chooseReceiverLabel.mas_left);
        make.top.equalTo(self.chooseReceiverLabel.mas_bottom);
        make.right.equalTo(self).with.offset(-ALIGN_WIDTH_PT(28));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(49)));
    }];
    
    [self.receiverImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.inputReceiverLabel.mas_centerY);
        make.right.equalTo(self).with.offset(-ALIGN_WIDTH_PT(13));
        make.size.equalTo(@(CGSizeMake(ALIGN_WIDTH_PT(14), ALIGN_HEIGHT_PT(16))));
    }];
    
    
    [self.receiverV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.chooseReceiverLabel.mas_left);
        make.right.equalTo(self.inputReceiverLabel.mas_right);
        make.top.equalTo(self.inputReceiverLabel.mas_bottom);
        make.height.equalTo(@(1));
    }];
    
    [self.chooseCSLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(ALIGN_WIDTH_PT(12));
        make.top.equalTo(self.receiverV.mas_bottom);
        make.height.equalTo(@(ALIGN_HEIGHT_PT(36)));
    }];
    
    [self.inputCSLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.chooseReceiverLabel.mas_left);
        make.top.equalTo(self.chooseCSLabel.mas_bottom);
        make.right.equalTo(self).with.offset(-ALIGN_WIDTH_PT(28));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(49)));
    }];
    
    [self.CSImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.inputCSLabel.mas_centerY);
        make.right.equalTo(self).with.offset(-ALIGN_WIDTH_PT(13));
        make.size.equalTo(@(CGSizeMake(ALIGN_WIDTH_PT(14), ALIGN_HEIGHT_PT(16))));
    }];
    
    
    [self.CSV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.chooseReceiverLabel.mas_left);
        make.right.equalTo(self.inputReceiverLabel.mas_right);
        make.top.equalTo(self.inputCSLabel.mas_bottom);
        make.height.equalTo(@(1));
    }];
    
    [self.noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(ALIGN_WIDTH_PT(12));
        make.top.equalTo(self.CSV.mas_bottom);
        make.height.equalTo(@(ALIGN_HEIGHT_PT(36)));
    }];
    
    [self.noteTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(ALIGN_WIDTH_PT(12));
        make.right.equalTo(self).with.offset(-ALIGN_WIDTH_PT(12));
        make.top.equalTo(self.noteLabel.mas_bottom);
        make.height.equalTo(@(ALIGN_HEIGHT_PT(90)));
    }];
    
    [self.photoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(ALIGN_WIDTH_PT(12));
        make.top.equalTo(self.noteTextF.mas_bottom);
        make.height.equalTo(@(ALIGN_HEIGHT_PT(36)));
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(ALIGN_WIDTH_PT(12));
        make.right.equalTo(self).with.offset(-ALIGN_WIDTH_PT(12));
        make.top.equalTo(self.photoLabel.mas_bottom);
        make.height.equalTo(@(ALIGN_HEIGHT_PT(250)));
    }];
    
    
    
}


#pragma mark -- 点击事件
-(void)respondsTo_chooseReceiver{ // 选择接收人
    
    [UIView animateWithDuration:0.5 animations:^{
        self.KwindowView.alpha = 0.3;
        self.alertView = [[HomeChooseAlertView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - ALIGN_HEIGHT_PT(600), SCREEN_WIDTH, ALIGN_HEIGHT_PT(600))];
        self.alertView.formInt = 1;
        self.alertView.delegate = self;
        [kWindow addSubview:self.alertView];
        
    }];
    
}

-(void)respondsTo_chooseCS{ // 选择抄送人
    
    [UIView animateWithDuration:0.5 animations:^{
        self.KwindowView.alpha = 0.3;
        self.alertView = [[HomeChooseAlertView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - ALIGN_HEIGHT_PT(600), SCREEN_WIDTH, ALIGN_HEIGHT_PT(600))];
        self.alertView.formInt = 2;
        self.alertView.delegate = self;
        [kWindow addSubview:self.alertView];
        
    }];
    
}


-(void)canleAlertView{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.KwindowView.alpha = 0;
        [self.alertView removeFromSuperview];
    }];
    
    
}
-(void)choosePerson:(NSString *)str UserId:(NSString *)userId formInt:(NSInteger)formInt{
    
    [self canleAlertView];
    if (formInt == 1){
        self.inputReceiverLabel.text = str;
        self.inputReceiverLabel.textColor = [UIColor blackColor];
        if (self.delegate && [self.delegate respondsToSelector:@selector(chooseReceiver:)]){
            [self.delegate chooseReceiver:userId];
        }
    }else{
        self.inputCSLabel.text = str;
        self.inputCSLabel.textColor = [UIColor blackColor];
        if (self.delegate && [self.delegate respondsToSelector:@selector(chooseCS:)]){
            [self.delegate chooseCS:userId];
            
        }
        
    }
}




#pragma amrk -- UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(inputNote:)]){
        [self.delegate inputNote:textField.text];
    }
    return YES;
}


-(void)textFieldDidEndEditing:(UITextField *)textField{
    [self textFieldShouldReturn:textField];
}



#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.photoArr.count + 1;
}
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell * sectionCell = nil;
    
    HomePublicChoosePhotoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomePublicChoosePhotoCollectionViewCell" forIndexPath:indexPath];
    cell.canleBtn.hidden = YES;
    if (indexPath.row == 0){
        cell.img.image = nil;
        cell.Addimg.image = [UIImage imageNamed:@"上传"];
    }else{
        cell.canleBtn.hidden = NO;
        NSString * str = [NSString stringWithFormat:@"%@",self.photoArr[indexPath.row - 1]];
        
        if ([str hasPrefix:@"http://"] || [str hasPrefix:@"https://"]){
            [cell.img sd_setImageWithURL:[NSURL URLWithString:str]];
        }else{
            cell.img.image = self.photoArr[indexPath.row - 1];
        }
        cell.dispachCanleBtn = ^{
            [self.photoArr removeObjectAtIndex:indexPath.row - 1];
            [self.uploadArr removeObjectAtIndex:indexPath.row - 1];
            [self.collectionView reloadData];
        };
    }
  
    sectionCell = cell;
        
        return sectionCell;
                
    
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0){
        [self pushTZimg];
    }else{
        SaTMNewWatermarkPhotoVc * vc = [SaTMNewWatermarkPhotoVc new];
        NSMutableArray * arr = @[].mutableCopy;
        [arr addObjectsFromArray:self.photoArr];
        [vc reloadImgWithFilesArray:arr WaterText:@"" Type:NO];
        [[SaViewControllerUtils currentViewController].navigationController pushViewController:vc animated:YES];
    }
}


-(void)pushTZimg{ // 选择照片
    
    
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc]initWithMaxImagesCount:9 columnNumber:4 delegate:self pushPhotoPickerVc:YES];
    //设置目前已经选中的图片数组
    imagePickerVc.allowPickingVideo = NO;
    imagePickerVc.allowTakeVideo = NO;
    imagePickerVc.showSelectedIndex = YES;
    imagePickerVc.selectedAssets = _selectedAssets;
    
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        [self.photoArr removeAllObjects];
        [self.photoArr addObjectsFromArray:photos];
        _selectedAssets = [NSMutableArray arrayWithArray:assets];
        [self.collectionView reloadData];

        self.uploadArr = @[].mutableCopy;
        
        [self.photoArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString * filename = [assets valueForKey:@"filename"];
            
    //         单张图片上传
            [self.uploadImgManager uploadImageWithParam:@{@"filename":filename} sourceImg:self.photoArr[idx] completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
                if (responseObject && [responseObject[@"code"] intValue] == 200){
                    [self.uploadArr addObject:responseObject[@"data"][@"url"]];
                    // 所有图片都上传完成后再处理代理
                    if (self.uploadArr.count == self.photoArr.count){
                        if (self.delegate && [self.delegate respondsToSelector:@selector(chooseUploadArr:)]){
                            [self.delegate chooseUploadArr:self.uploadArr];
                        }
                    }
                }
            }];
        }];
        
        
        
    }];
    imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
    [[SaViewControllerUtils currentViewController] presentViewController:imagePickerVc animated:YES completion:nil];

}




#pragma mark - item宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

//    return CGSizeMake(SCREEN_WIDTH, ALIGN_HEIGHT_PT(200));
    return CGSizeMake((SCREEN_WIDTH - ALIGN_WIDTH_PT(45)) / 3, (SCREEN_WIDTH - ALIGN_WIDTH_PT(45)) / 3);
    
}

#pragma mark - <UICollectionViewDelegateFlowLayout>
#pragma mark - X间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return  ALIGN_WIDTH_PT(0);
}
#pragma mark - Y间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return  ALIGN_WIDTH_PT(10);
}





#pragma mark -- 懒加载
-(UILabel *)chooseReceiverLabel{
    if (!_chooseReceiverLabel){
        _chooseReceiverLabel = [UILabel new];
        _chooseReceiverLabel.text = @"选择接受人";
        _chooseReceiverLabel.font = [UIFont systemFontOfSize:16 weight:0.5];
        _chooseReceiverLabel.textAlignment = NSTextAlignmentLeft;
        _chooseReceiverLabel.textColor = [UIColor blackColor];
    }
    return _chooseReceiverLabel;
}

-(UILabel *)inputReceiverLabel{
    if (!_inputReceiverLabel){
        _inputReceiverLabel = [UILabel new];
        _inputReceiverLabel.text = @"请输入姓名查询";
        _inputReceiverLabel.textColor = COLOR_RGB(180, 180, 180);
        _inputReceiverLabel.textAlignment = NSTextAlignmentLeft;
        _inputReceiverLabel.font = FONT14;
        _inputReceiverLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(respondsTo_chooseReceiver)];
        [_inputReceiverLabel addGestureRecognizer:ges];
    }
    return _inputReceiverLabel;
}

-(UIImageView *)receiverImg{
    if (!_receiverImg){
        _receiverImg = [UIImageView new];
        _receiverImg.image = [UIImage imageNamed:@"向右箭头"];
    }
    return _receiverImg;
}
-(UIView *)receiverV{
    if (!_receiverV){
        _receiverV = [UIView  new];
        _receiverV.backgroundColor = COLOR_RGB(242, 242, 242);
    }
    return _receiverV;
}

-(UILabel *)chooseCSLabel{
    if (!_chooseCSLabel){
        _chooseCSLabel = [UILabel new];
        _chooseCSLabel.text = @"选择抄送人";
        _chooseCSLabel.font = [UIFont systemFontOfSize:16 weight:0.5];
        _chooseCSLabel.textAlignment = NSTextAlignmentLeft;
        _chooseCSLabel.textColor = [UIColor blackColor];
        
    }
    return _chooseCSLabel;
}

-(UILabel *)inputCSLabel{
    if (!_inputCSLabel){
        _inputCSLabel = [UILabel new];
        _inputCSLabel.text = @"请输入姓名查询";
        _inputCSLabel.textColor = COLOR_RGB(180, 180, 180);
        _inputCSLabel.textAlignment = NSTextAlignmentLeft;
        _inputCSLabel.font = FONT14;
        _inputCSLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(respondsTo_chooseCS)];
        [_inputCSLabel addGestureRecognizer:ges];
    }
    return _inputCSLabel;
}

-(UIImageView *)CSImg{
    if (!_CSImg){
        _CSImg = [UIImageView new];
        _CSImg.image = [UIImage imageNamed:@"向右箭头"];
    }
    return _CSImg;
}
-(UIView *)CSV{
    if (!_CSV){
        _CSV = [UIView  new];
        _CSV.backgroundColor = COLOR_RGB(242, 242, 242);
    }
    return _CSV;
}

-(UILabel *)noteLabel{
    if (!_noteLabel){
        _noteLabel = [UILabel new];
        _noteLabel.text = @"转单说明";
        _noteLabel.font = [UIFont systemFontOfSize:16 weight:0.5];
        _noteLabel.textAlignment = NSTextAlignmentLeft;
        _noteLabel.textColor = [UIColor blackColor];
    }
    return _noteLabel;
}

-(UITextField *)noteTextF{
    if (!_noteTextF){
        _noteTextF = [UITextField new];
        _noteTextF.placeholder = @"请输入转单理由及工具、货物、危险品等滞留区域";
        _noteTextF.font = FONT14;
        _noteTextF.textAlignment = NSTextAlignmentLeft;
        _noteTextF.backgroundColor = COLOR_RGB(242, 242, 242);
        _noteTextF.layer.cornerRadius = 5;
        _noteTextF.layer.masksToBounds = YES;
        _noteTextF.delegate = self;
    }
    return _noteTextF;
}


-(UILabel *)photoLabel{
    if (!_photoLabel){
        _photoLabel = [UILabel new];
        _photoLabel.text = @"照片";
        _photoLabel.font = [UIFont systemFontOfSize:16 weight:0.5];
        _photoLabel.textAlignment = NSTextAlignmentLeft;
        _photoLabel.textColor = [UIColor blackColor];
    }
    return _photoLabel;
}



-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        
        //cell
        [_collectionView registerClass:[HomePublicChoosePhotoCollectionViewCell class] forCellWithReuseIdentifier:@"HomePublicChoosePhotoCollectionViewCell"];
        
    }
    return _collectionView;
}

-(NSMutableArray *)photoArr{
    if (!_photoArr){
        _photoArr = [NSMutableArray array];
    }
    return _photoArr;
}

-(HomeUploadFileNetWorking *)uploadImgManager{
    if (!_uploadImgManager){
        _uploadImgManager = [HomeUploadFileNetWorking new];
    }
    return _uploadImgManager;
}




    
@end
